package com.ym.b2b.type.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ym.b2b.commons.controller.BaseController;
import com.ym.b2b.type.service.EmBNewsTypeService;
import com.ym.b2b.type.sqlbean.EmBNewsType;
import com.zc.commons.base.Result;

@Controller
@RequestMapping("/newstype")
public class ManagerNewsTypeController extends BaseController {

    @Autowired
    private EmBNewsTypeService emBNewsTypeService;

    /**
     * @Title: showCmsTypeDetail
     * @Description: 获取内容类型 明细 所有字段
     */
    @RequestMapping("showCmsTypeDetail.do")
    @ResponseBody
    public Result showCmsTypeDetail(Long newsTypeId) {
        Result ret = new Result();
        try {
            EmBNewsType e = emBNewsTypeService.showCmsTypeDetail(newsTypeId);
            ret.putData("detail", e);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, "获取内容失败!");
        }
        return ret;

    }

    @RequestMapping("searchEmBNewsType.do")
    @ResponseBody
    public EmBNewsType searchEmBNewsType(EmBNewsType emBNewsType, HttpServletRequest req) {
        EmBNewsType result = new EmBNewsType();
        result = emBNewsTypeService.searchEmBNewsType(emBNewsType);
        return result;
    }

    @RequestMapping("searchEmBNewsTypeByParentId.do")
    @ResponseBody
    public Map searchEmBNewsTypeByParentId(EmBNewsType emBNewsType, HttpServletRequest req) {
        List<EmBNewsType> list = new ArrayList<EmBNewsType>();
        list = emBNewsTypeService.searchEmBNewsTypeByParentId(emBNewsType);
        Map map = new HashMap();
        map.put("rows", list);
        return map;
    }

    @RequestMapping("searchEmBNewsTypeByParentIdToComboTree.do")
    @ResponseBody
    public List searchEmBNewsTypeByParentIdToTree(EmBNewsType emBNewsType, HttpServletRequest req) {
        List<EmBNewsType> list = new ArrayList<EmBNewsType>();
        list = emBNewsTypeService.searchEmBNewsTypeByParentIdToComboTree(emBNewsType);
        return list;
    }

    // 新增记录
    @RequestMapping("insertNewsType.do")
    @ResponseBody
    public Result insertNewsType(EmBNewsType emBNewsType, HttpServletRequest req) {
        Result result = new Result();
        result = emBNewsTypeService.insertNewsType(emBNewsType);
        return result;
    }

    // 更新记录
    @RequestMapping("updateEmBNewsType.do")
    @ResponseBody
    public Result updateEmBNewsType(EmBNewsType emBNewsType, HttpServletRequest req) throws DataAccessException {
        Result result = new Result();
        result = emBNewsTypeService.updateEmBNewsType(emBNewsType);
        return result;
    }

    // 删除记录内容
    @RequestMapping("deleteEmBNewsType.do")
    @ResponseBody
    public Result deleteEmBNewsType(EmBNewsType emBNewsType, HttpServletRequest req) {
        Result result = new Result();
        try {
            emBNewsTypeService.deleteEmBNews(emBNewsType.getNewsTypeId());
        } catch (Exception e) {
            // TODO: handle exception
            result.setCode("100");
            return result;
        }
        result.setCode("200");
        return result;
    }

}
